package cn.gjculture.shopping.api.service.impl;


import cn.gjculture.shopping.api.dao.FileDao;
import cn.gjculture.shopping.api.entity.FileEntity;
import cn.gjculture.shopping.api.service.FileService;
import cn.gjculture.shopping.api.util.DateUtil;
import cn.gjculture.shopping.api.util.FtpClient;
import cn.gjculture.shopping.api.util.UUIDUtil;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

@Service
public class FileServiceImpl implements FileService {
    @Value("${file.server.url}")
    private String fileServerUrl;

    @Value("${file.image.path}")
    private String fileImagePath;
    @Autowired
    private FileDao fileDao;

    @Autowired
    FtpClient ftpClient;

    @Transactional
    @Override
    public FileEntity saveFile(MultipartFile file, String type) {
        FileEntity fileEntity=new FileEntity();
        fileEntity.setFileId(UUIDUtil.getUUID());
        fileEntity.setFileName(file.getOriginalFilename());
        String suffix = fileEntity.getFileName().substring( fileEntity.getFileName().lastIndexOf("."));
        String filePath=fileImagePath + "/" + type;
        String fileName=fileEntity.getFileId()+suffix;
        fileEntity.setFilePath(filePath + "/" + fileName);
        fileEntity.setServerUrl(fileServerUrl+"/"+type+"/"+fileName);
        fileEntity.setStatus(1);
        File bufferFile=new File(fileName);
        OutputStream bos=null;
        try {
            bos=new BufferedOutputStream(new FileOutputStream(bufferFile),512);
            if("thumb".equals(type)){
                Thumbnails.of(file.getInputStream()).scale(1f).outputQuality(0.25f).toOutputStream(bos);
                ftpClient.uploadFile(fileName,filePath,new BufferedInputStream(new FileInputStream(bufferFile)));
            }else{
                ftpClient.uploadFile(fileName,filePath,file.getInputStream());
            }

        }catch (Exception e) {
            throw new RuntimeException(e.getCause());
        }finally {
            if(bos!=null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bufferFile!=null&&bufferFile.exists()){
                bufferFile.delete();
            }
        }

        fileEntity.setInsertTime(DateUtil.getTimestamp());
        fileEntity.setUpdateTime(DateUtil.getTimestamp());
        fileDao.insert(fileEntity);

        return fileEntity;
    }

    @Transactional
    @Override
    public boolean delFile(String fileId) {
        FileEntity fileEntity=fileDao.queryById(fileId);
        if(fileEntity!=null){
            ftpClient.delFile(fileEntity.getFilePath());
            fileDao.delete(fileId);
            return true;
        }
        return false;
    }

    @Override
    public int delFiles(String[] fileIds) {
        if(fileIds!=null&& fileIds.length>0){
            for(String id:fileIds){
                delFile(id);
            }

            return fileIds.length;
        }
        return 0;
    }
}
